package com.hpsk.webSocket.model.ai.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hpsk.common.CommonException;
import com.hpsk.webSocket.model.ai.dto.BizAiMessageParamDto;
import com.hpsk.webSocket.model.ai.entity.BizAiMessage;
import com.hpsk.webSocket.model.ai.dao.BizAiMessageMapper;
import com.hpsk.webSocket.model.ai.entity.BizAiMessageContent;
import com.hpsk.webSocket.model.ai.service.IBizAiMessageContentService;
import com.hpsk.webSocket.model.ai.service.IBizAiMessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hpsk.webSocket.model.ai.vo.BizAiMessageVo;
import io.github.linpeilie.Converter;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户消息体 服务实现类
 * </p>
 *
 * @author huangwang
 * @since 2025-05-20
 */
@Service
@AllArgsConstructor(onConstructor_ = @__(@Autowired))
public class BizAiMessageServiceImpl extends ServiceImpl<BizAiMessageMapper, BizAiMessage> implements IBizAiMessageService {

    private final Converter converter;

    private final IBizAiMessageContentService bizAiMessageContentService;

    @Override
    public BizAiMessage add(BizAiMessageParamDto dto) {
        BizAiMessage convert = converter.convert(dto, BizAiMessage.class);
        save(convert);
        return convert;
    }

    @Override
    public Boolean deleteById(Long messageId, String userId) {
        BizAiMessageVo detail = detail(messageId);
        if (Long.valueOf(userId).equals(detail.getUserId())) {
            removeById(messageId);
            bizAiMessageContentService.remove(Wrappers.<BizAiMessageContent>lambdaQuery().eq(BizAiMessageContent::getMessageId, messageId));
            return true;
        } else {
            throw new CommonException("无权限删除！");
        }
    }

    @Override
    public BizAiMessageVo detail(Long id) {
        BizAiMessage one = getOne(Wrappers.<BizAiMessage>lambdaQuery().eq(BizAiMessage::getId, id));
        if (one == null) {
            throw new CommonException("消息不存在！");
        }
        return converter.convert(one, BizAiMessageVo.class);
    }
}
